package com.shioirikukuri.controller;

import com.shioirikukuri.entity.User;
import com.shioirikukuri.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/users")
public class UserController {

	@Autowired
	private IUserService userService;

	/**
	 * 获取所有用户信息
	 * @return 所有用户的列表
	 */
	@GetMapping
	public ResponseEntity<List<User>> getAllUsers() {
		List<User> users = userService.getAllUsers();
		return ResponseEntity.ok(users);
	}

	/**
	 * 获取所有消费者用户信息
	 * @return 所有消费者用户的列表
	 *
	 */
	@GetMapping("/consumers")
	public ResponseEntity<List<User>> getAllConsumers() {
		List<User> users = userService.getAllConsumers();
		return ResponseEntity.ok(users);
	}

	/**
	 * 根据ID获取用户信息
	 * @param id 用户ID
	 * @return 匹配的用户实体或404响应
	 */
	@GetMapping("/{id}")
	public ResponseEntity<User> getUserById(@PathVariable Long id) {
		Optional<User> user = userService.getUserById(id);
		if (user.isPresent()) {
			return ResponseEntity.ok(user.get());
		} else {
			return ResponseEntity.notFound().build();
		}
	}

	/**
	 * 创建一个新用户
	 * @param user 新用户的详细信息
	 * @return 创建成功的用户实体和201状态码
	 */
	@PostMapping
	public ResponseEntity<User> createUser(@RequestBody User user) {
		User createdUser = userService.addUser(user);
		return ResponseEntity.status(201).body(createdUser);
	}

	/**
	 * 更新指定ID的用户信息
	 * @param id 用户ID
	 * @param userDetails 新的用户详情
	 * @return 更新后的用户实体或404响应
	 */
	@PutMapping("/{id}")
	public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User userDetails) {
		Optional<User> optionalUser = userService.getUserById(id);
		if (optionalUser.isPresent()) {
			User updatedUser = userService.updateUser(userDetails);
			return ResponseEntity.ok(updatedUser);
		} else {
			return ResponseEntity.notFound().build();
		}
	}

	/**
	 * 删除指定ID的用户
	 * @param id 用户ID
	 * @return 204 No Content 响应或404响应
	 */
	@DeleteMapping("/{id}")
	public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
		if (userService.getUserById(id).isPresent()) {
			userService.deleteUser(id);
			return ResponseEntity.noContent().build();
		} else {
			return ResponseEntity.notFound().build();
		}
	}

	/**
	 * 检查用户是否被加入黑名单
	 * @param phoneNumber 用户手机号
	 * @return 用户是否被加入黑名单的状态
	 */
	@GetMapping("/blacklisted/{phoneNumber}")
	public ResponseEntity<Boolean> isUserBlacklisted(@PathVariable String phoneNumber) {
		Boolean blacklisted = userService.isUserBlacklisted(phoneNumber);
		return ResponseEntity.ok(blacklisted);
	}

	/**
	 * 将用户加入黑名单
	 * @param id 用户ID
	 * @return 204 No Content 响应或404响应
	 */
	@PostMapping("/{id}/blacklist")
	public ResponseEntity<Void> blacklistUser(@PathVariable Long id) {
		if (userService.getUserById(id).isPresent()) {
			userService.blacklistUser(id);
			return ResponseEntity.noContent().build();
		} else {
			return ResponseEntity.notFound().build();
		}
	}

	/**
	 * 将用户从黑名单移除
	 * @param id 用户ID
	 * @return 204 No Content 响应或404响应
	 */
	@DeleteMapping("/{id}/blacklist")
	public ResponseEntity<Void> unblacklistUser(@PathVariable Long id) {
		if (userService.getUserById(id).isPresent()) {
			userService.unblacklistUser(id);
			return ResponseEntity.noContent().build();
		} else {
			return ResponseEntity.notFound().build();
		}
	}
}